﻿package com.bizmlib.framework.flashapp.mvc
{
	import flash.events.Event;
	
	import com.bizmlib.framework.abstract.mvc.AbstractView;
	import com.bizmlib.framework.flashapp;
	import com.bizmlib.framework.flashapp.mvc.FlashAppModel;
	
	
	public class FlashAppView extends AbstractView
	{
		/**
		 * Indicates whether a class should subscribe to a stage "resize" event. Must be set to true in a subclass
		 * before a FlashAppView's init method is invoked. A good place for that is a overriden init function in a 
		 * subclass. Of course you can set it many times in different places but in that case a behaviour would be
		 * unpredictable.
		 * A value of this property affects a added to/removed from stage events listening behaviour.
		 */
		protected var subscribeToStageResizeEvent:Boolean;
		
		
		public function FlashAppView(/**/appModel:FlashAppModel)
		{
			super(appModel);
		}
		
		
		/**
		 * The model getter is overriden in order to return an actual type.
		 *
		flashapp public function get model():FlashAppModel
		{
			return super.model as FlashAppModel;
		}
		/**/
		
		
		override public function clean():void
		{
			super.clean();
			
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		/**
		 * A method which was in place previously. Not sure if i should remove it completely as a stageResize method
		 * has been added. Commenting in for now.
		 */
		//public function onResize():void
		//{
		//}
	
	
		override protected function init():void
		{
			super.init();
			
			if(subscribeToStageResizeEvent)
			{
				if(stage)
				{
					addedToStage(null);
				}
				else
				{
					this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
				}
			}
		}
		/**
		 * Is invoked from the init method of the base class.
		 */
		override protected function subscribeToModelEvents():void
		{
			//trace(this + ".subscribeToModelsEvents()");
			super.subscribeToModelEvents();
		}
		
		/**
		 * Handles a 'addedToStage' event if it was required (check the properties affecting a added to/removed from
		 * stage events listening behaviour).
		 */
		protected function addedToStage(event:Event = null):void
		{
			if(subscribeToStageResizeEvent)
			{
				stage.addEventListener(Event.RESIZE, stageResize);
				stageResize(new Event(Event.RESIZE, false, false));
			}
			
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		/**
		 * Handles a 'removedFromStage' event in case a instance has been subscribed to list to a 'addedToStage' event.
		 */
		protected function removedFromStage(event:Event):void
		{
			stage.removeEventListener(Event.RESIZE, stageResize);
			
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		/**
		 * Handles the stage resize event.
		 */
		protected function stageResize(event:Event):void
		{
			//trace(this + ".onResize()");
		}
		
		
		/**
		 * Returns a string describing an instance.
		 */
		override public function toString():String
		{
			return "[com.bizmlib.framework.flashapp.mvc.FlashAppView]";
		}
	}
}